package com.hzh.cusviewdemo.Music;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;
import com.hzh.cusviewdemo.Bean.MyMusicData;
import com.hzh.cusviewdemo.Util.Config;
import com.hzh.cusviewdemo.Util.MmkvUtlis;
import com.hzh.cusviewdemo.Util.SugarUtils;

import java.util.List;

public class MyMusicUtil {

    // 设置值
    public static void setShared(String key, int value) {
        MmkvUtlis.getInstance().setValues(key, String.valueOf(value));
    }

    public static void setShared(String key, String value) {
        MmkvUtlis.getInstance().setValues(key, value);
    }

    // 获取值
    public static int getIntShared(String key) {
        if (MmkvUtlis.getInstance().getValues(key) == null) {
            return -1;
        }
        return Integer.parseInt(MmkvUtlis.getInstance().getValues(key));
    }

    public static String getStringShared(String key) {
        if (MmkvUtlis.getInstance().getValues(key) == null) {
            return "";
        }
        return MmkvUtlis.getInstance().getValues(key);
    }


    /**
     * 播放上一首
     *
     * @param context
     */
    public static void playPreMusic(Context context) {
        //获取下一首ID
        String musicId = getPreMusicId();
        MyMusicUtil.setShared(Constant.KEY_ID, musicId);
        if (musicId.equals("")) {
            Intent intent = new Intent(MusicPlayService.PLAYER_MANAGER_ACTION);
            intent.putExtra(Constant.COMMAND, Constant.COMMAND_STOP);
            context.sendBroadcast(intent);
            ToastUtils.showShort("播放失败，歌曲不存在！");
            return;
        }
        //获取播放歌曲路径
        MyMusicData musicDataByRid = SugarUtils.findMusicDataByRidOrSid(musicId);
        String path = musicDataByRid.getUrl();
        Log.d(Config.TAG, "MMU:" + "准备播放的上一首歌曲 名称 =" + musicDataByRid.getName() + " 上一首歌曲 path =" + path);
        //发送播放请求
        Intent intent = new Intent(MusicPlayService.PLAYER_MANAGER_ACTION);
        MyMusicUtil.setShared(Constant.KEY_NAME, musicDataByRid.getName());
        MyMusicUtil.setShared(Constant.KEY_SONGER, musicDataByRid.getSonger());
        MyMusicUtil.setShared(Constant.KEY_PIC, musicDataByRid.getPic());
        intent.putExtra(Constant.COMMAND, Constant.COMMAND_PLAY);
        intent.putExtra(Constant.KEY_PATH, path);
        intent.putExtra(Constant.KEY_ID, musicId);
        intent.putExtra(Constant.KEY_TITLE, musicDataByRid.getTitle());
        context.sendBroadcast(intent);
    }

    /**
     * 自动播放下一首歌曲
     *
     * @param context
     */
    public static void playNextMusic(Context context) {
        //获取下一首ID
        String musicId = getNextMusicId(false);
//        String nextMusicUrl = getNextMusicUrl(false);
        MyMusicUtil.setShared(Constant.KEY_ID, musicId);
        if (musicId.equals("")) {
            Intent intent = new Intent(MusicPlayService.PLAYER_MANAGER_ACTION);
            intent.putExtra(Constant.COMMAND, Constant.COMMAND_STOP);
            context.sendBroadcast(intent);
            ToastUtils.showShort("播放失败，歌曲不存在！");
            return;
        }
        //获取播放歌曲路径
        MyMusicData musicDataByRid = SugarUtils.findMusicDataByRidOrSid(musicId);
        String path = musicDataByRid.getUrl();
        Log.d(Config.TAG, "MMU:" + "下一首歌曲 名称 =" + musicDataByRid.getName() + " 下一首歌曲 path =" + path);
        //发送播放请求 先释放
        Intent intent = new Intent(MusicPlayService.PLAYER_MANAGER_ACTION);
        intent.putExtra(Constant.COMMAND, Constant.COMMAND_RELEASE);
        context.sendBroadcast(intent);

        MyMusicUtil.setShared(Constant.KEY_NAME, musicDataByRid.getName());
        MyMusicUtil.setShared(Constant.KEY_SONGER, musicDataByRid.getSonger());
        MyMusicUtil.setShared(Constant.KEY_PIC, musicDataByRid.getPic());
        intent.putExtra(Constant.COMMAND, Constant.COMMAND_PLAY);
        intent.putExtra(Constant.KEY_PATH, path);
        intent.putExtra(Constant.KEY_ID, musicId);
        intent.putExtra(Constant.KEY_TITLE, musicDataByRid.getTitle());
        context.sendBroadcast(intent);
    }

    /**
     * 点击播放下一首
     *
     * @param context
     */
    public static void playClickNextMusic(Context context) {
        //获取下一首ID  false 自动播放下一首，会看播放选项是否随机顺序等等
        String musicId = getNextMusicId(false);
        MyMusicUtil.setShared(Constant.KEY_ID, musicId);
        if (musicId.equals("")) {
            Intent intent = new Intent(MusicPlayService.PLAYER_MANAGER_ACTION);
            intent.putExtra(Constant.COMMAND, Constant.COMMAND_STOP);
            context.sendBroadcast(intent);
            ToastUtils.showShort("播放失败，歌曲不存在！");
            return;
        }
        //获取播放歌曲路径
        MyMusicData musicDataByRid = SugarUtils.findMusicDataByRidOrSid(musicId);
        String path = musicDataByRid.getUrl();
        Log.d(Config.TAG, "MMU:" + "下一首歌曲 名称 =" + musicDataByRid.getName() + " 下一首歌曲 path =" + path);
        //发送播放请求
        Intent intent = new Intent(MusicPlayService.PLAYER_MANAGER_ACTION);
        intent.putExtra(Constant.COMMAND, Constant.COMMAND_RELEASE);
        context.sendBroadcast(intent);

        MyMusicUtil.setShared(Constant.KEY_NAME, musicDataByRid.getName());
        MyMusicUtil.setShared(Constant.KEY_SONGER, musicDataByRid.getSonger());
        MyMusicUtil.setShared(Constant.KEY_PIC, musicDataByRid.getPic());
        intent.putExtra(Constant.COMMAND, Constant.COMMAND_PLAY);
        intent.putExtra(Constant.KEY_PATH, path);
        intent.putExtra(Constant.KEY_ID, musicId);
        intent.putExtra(Constant.KEY_TITLE, musicDataByRid.getTitle());
        context.sendBroadcast(intent);
    }

    public static String getNextMusicId(boolean isClick) {
        // 获取现在正在播放的Id
        int curIndex = 0;
        String nextMusicId = "";
        String musicId = MyMusicUtil.getStringShared(Constant.KEY_ID);

        // 根据当前播放的歌单确定下一首歌曲来源
        String nowMusicMenu = MyMusicUtil.getStringShared(Constant.KEY_NOW_PLAY_MENU);
        List<MyMusicData> musicData = SugarUtils.findMusicDataByMenu(nowMusicMenu);
        // 获取现在的播放列表
//        List<MyMusicData> musicData = SugarUtils.findMusicData();

        if (isClick) {
            for (int i = 0; i < musicData.size(); i++) {
                if (musicData.get(i).getSid().equals(musicId)) {
                    curIndex = i;
                }
            }
            if (musicData.size() == 0) {
                return "";
            }
            if ((curIndex + 1) == musicData.size()) {
                // 就一首歌
                nextMusicId = musicData.get(0).getSid();
            } else {
                ++curIndex;
                nextMusicId = musicData.get(curIndex).getSid();
            }
            return nextMusicId;
        } else {
            // 按照播放模式，确定下一首歌曲，获取播放模式
            int playMode = MyMusicUtil.getIntShared(Constant.KEY_MODE);
            switch (playMode) {
                case Constant.PLAYMODE_SEQUENCE:
                    for (int i = 0; i < musicData.size(); i++) {
                        if (musicData.get(i).getSid().equals(musicId)) {
                            curIndex = i;
                        }
                    }
                    if (musicData.size() == 0) {
                        return "";
                    }
                    if ((curIndex + 1) == musicData.size()) {
                        // 就一首歌
                        nextMusicId = musicData.get(0).getSid();
                    } else {
                        ++curIndex;
                        nextMusicId = musicData.get(curIndex).getSid();
                    }
                    break;
                case Constant.PLAYMODE_SINGLE_REPEAT:
                    nextMusicId = musicId;
                    break;
                case Constant.PLAYMODE_RANDOM:
                    nextMusicId = getRandomMusic(musicData, musicId);
                    break;
            }
            return nextMusicId;
        }
    }

    /**
     * 得到下一首需要播放的歌曲
     *
     * @param isClick 是否人为点击切换
     * @return
     */
    public static String getNextMusicUrl(boolean isClick) {
        // 获取现在正在播放的Id
        int curIndex = 0;
        String musicUrl = MyMusicUtil.getStringShared(Constant.KEY_PATH);
        // 获取播放列表
        List<MyMusicData> musicData = SugarUtils.findMusicData();
        if (musicData.size() == 0) {
            return "";
        }
        if (isClick) {
            for (int i = 0; i < musicData.size(); i++) {
                if (musicData.get(i).getUrl().equals(musicUrl)) {
                    curIndex = i;
                }
            }
            if ((curIndex + 1) == musicData.size()) {
                // 就一首歌
                musicUrl = musicData.get(0).getUrl();
            } else {
                ++curIndex;
                musicUrl = musicData.get(curIndex).getUrl();
            }
            return musicUrl;
        } else {
            // 获取播放模式
            int playMode = MyMusicUtil.getIntShared(Constant.KEY_MODE);
            switch (playMode) {
                case Constant.PLAYMODE_SEQUENCE:
                    for (int i = 0; i < musicData.size(); i++) {
                        if (musicData.get(i).getUrl().equals(musicUrl)) {
                            curIndex = i;
                        }
                    }
                    if (musicData.size() == 0) {
                        return "";
                    }
                    if ((curIndex + 1) == musicData.size()) {
                        // 就一首歌
                        musicUrl = musicData.get(0).getUrl();
                    } else {
                        ++curIndex;
                        musicUrl = musicData.get(curIndex).getUrl();
                    }
                    break;
                case Constant.PLAYMODE_SINGLE_REPEAT:
                    break;
                case Constant.PLAYMODE_RANDOM:
                    musicUrl = getRandomMusicByUrl(musicData, musicUrl);
                    break;
            }
            return musicUrl;
        }
    }

    // 获取随机歌曲ID
    private static String getRandomMusic(List<MyMusicData> musicData, String id) {
        String musicId;
        if (musicData.isEmpty()) {
            return "";
        }
        if (musicData.size() == 1) {
            return id;
        }
        do {
            int count = (int) (Math.random() * musicData.size());
            musicId = musicData.get(count).getSid();
        } while (musicId.equals(id));
        return musicId;
    }

    private static String getRandomMusicByUrl(List<MyMusicData> musicData, String musicUrl) {
        String url;
        if (musicData.isEmpty()) {
            return "";
        }
        if (musicData.size() == 1) {
            return musicUrl;
        }
        do {
            int count = (int) (Math.random() * musicData.size());
            url = musicData.get(count).getUrl();
        } while (url.equals(musicUrl));
        return url;
    }

    /**
     * 获取上一首歌曲
     *
     * @return
     */
    public static String getPreMusicId() {
        // 获取现在正在播放的Id
        int curIndex = 0;
        String preMusicId = "";
        String musicId = MyMusicUtil.getStringShared(Constant.KEY_ID);

        String nowMusicMenu = MyMusicUtil.getStringShared(Constant.KEY_NOW_PLAY_MENU);
        List<MyMusicData> musicData = SugarUtils.findMusicDataByMenu(nowMusicMenu);

        // 获取现在的播放列表
//        List<MyMusicData> musicData = SugarUtils.findMusicData();
        // 获取播放模式
//        int playMode = MyMusicUtil.getIntShared(Constant.KEY_MODE);
//        switch (playMode) {
//            case Constant.PLAYMODE_SEQUENCE:
//
//                break;
//            case Constant.PLAYMODE_SINGLE_REPEAT:
//                preMusicId = musicId;
//                break;
//            case Constant.PLAYMODE_RANDOM:
//                preMusicId = getRandomMusic(musicData, musicId);
//                break;
//        }
        for (int i = 0; i < musicData.size(); i++) {
            if (musicData.get(i).getSid().equals(musicId)) {
                curIndex = i;
            }
        }
        if (musicData.size() == 0) {
            return "";
        }
        if (curIndex == 0) {
            preMusicId = musicData.get(musicData.size() - 1).getSid();
        } else {
            --curIndex;
            preMusicId = musicData.get(curIndex).getSid();
        }
        return preMusicId;
    }

    public static String getPreMusicUrl() {
        // 获取现在正在播放的Id
        int curIndex = 0;
        String preMusicUrl = "";
        String musicUrl = MyMusicUtil.getStringShared(Constant.KEY_PATH);
        // 获取现在的播放列表
        List<MyMusicData> musicData = SugarUtils.findMusicData();
        // 获取播放模式
        int playMode = MyMusicUtil.getIntShared(Constant.KEY_MODE);
        switch (playMode) {
            case Constant.PLAYMODE_SEQUENCE:
                for (int i = 0; i < musicData.size(); i++) {
                    if (musicData.get(i).getUrl().equals(musicUrl)) {
                        curIndex = i;
                    }
                }
                if (musicData.size() == 0) {
                    return "";
                }
                if (curIndex == 0) {
                    preMusicUrl = musicData.get(musicData.size() - 1).getUrl();
                } else {
                    --curIndex;
                    preMusicUrl = musicData.get(curIndex).getUrl();
                }
                break;
            case Constant.PLAYMODE_SINGLE_REPEAT:
                preMusicUrl = musicUrl;
                break;
            case Constant.PLAYMODE_RANDOM:
                preMusicUrl = getRandomMusicByUrl(musicData, musicUrl);
                break;
        }
        return preMusicUrl;
    }


}
